<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="chrome=1">

    

    <title>
      DOM事件 | 江月夜 
    </title>

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    
      <meta name="author" content="江月夜">
    
    

    <meta name="description" content="事件级别 DOM0 element.onclick = function(){} DOM2 element.addEventListener(&amp;#39;click&amp;#39;, function(){}, false) DOM3 element.addEventListener(&amp;#39;keyu">
<meta name="keywords" content="前端">
<meta property="og:type" content="article">
<meta property="og:title" content="DOM事件 | 江月夜">
<meta property="og:url" content="http://yoursite.com/2018/09/26/event/index.html">
<meta property="og:site_name" content="江月夜">
<meta property="og:description" content="事件级别 DOM0 element.onclick = function(){} DOM2 element.addEventListener(&amp;#39;click&amp;#39;, function(){}, false) DOM3 element.addEventListener(&amp;#39;keyup&amp;#39;, function(){}, false)在DOM2事件后新增了UI事件、鼠标事件、键盘事">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://yoursite.com/images/eventflow.png">
<meta property="og:updated_time" content="2018-10-29T09:06:10.153Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="DOM事件 | 江月夜">
<meta name="twitter:description" content="事件级别 DOM0 element.onclick = function(){} DOM2 element.addEventListener(&amp;#39;click&amp;#39;, function(){}, false) DOM3 element.addEventListener(&amp;#39;keyup&amp;#39;, function(){}, false)在DOM2事件后新增了UI事件、鼠标事件、键盘事">
<meta name="twitter:image" content="http://yoursite.com/images/eventflow.png">
    
    
    
      <link rel="icon" type="image/x-icon" href="/favicon.png">
    
    <link rel="stylesheet" href="/css/uno.css">
    <link rel="stylesheet" href="/css/highlight.css">
    <link rel="stylesheet" href="/css/archive.css">
    <link rel="stylesheet" href="/css/china-social-icon.css">

</head>
<body>

    <span class="mobile btn-mobile-menu">
        <i class="icon icon-list btn-mobile-menu__icon"></i>
        <i class="icon icon-x-circle btn-mobile-close__icon hidden"></i>
    </span>

    

<header class="panel-cover panel-cover--collapsed">


  <div class="panel-main">

  
    <div class="panel-main__inner panel-inverted">
    <div class="panel-main__content">

        

        <h1 class="panel-cover__title panel-title"><a href="/" title="link to homepage">江月夜</a></h1>
        <hr class="panel-cover__divider" />

        
        <p class="panel-cover__description">
          腹有诗书气自华
        </p>
        <hr class="panel-cover__divider panel-cover__divider--secondary" />
        

        <div class="navigation-wrapper">

          <nav class="cover-navigation cover-navigation--primary">
            <ul class="navigation">

              
                
                <li class="navigation__item"><a href="/#blog" title="" class="blog-button">首页</a></li>
              
                
                <li class="navigation__item"><a href="/about" title="" class="">关于</a></li>
              
                
                <li class="navigation__item"><a href="/archive" title="" class="">归档</a></li>
              

            </ul>
          </nav>

          <!-- ----------------------------
To add a new social icon simply duplicate one of the list items from below
and change the class in the <i> tag to match the desired social network
and then add your link to the <a>. Here is a full list of social network
classes that you can use:

    icon-social-500px
    icon-social-behance
    icon-social-delicious
    icon-social-designer-news
    icon-social-deviant-art
    icon-social-digg
    icon-social-dribbble
    icon-social-facebook
    icon-social-flickr
    icon-social-forrst
    icon-social-foursquare
    icon-social-github
    icon-social-google-plus
    icon-social-hi5
    icon-social-instagram
    icon-social-lastfm
    icon-social-linkedin
    icon-social-medium
    icon-social-myspace
    icon-social-path
    icon-social-pinterest
    icon-social-rdio
    icon-social-reddit
    icon-social-skype
    icon-social-spotify
    icon-social-stack-overflow
    icon-social-steam
    icon-social-stumbleupon
    icon-social-treehouse
    icon-social-tumblr
    icon-social-twitter
    icon-social-vimeo
    icon-social-xbox
    icon-social-yelp
    icon-social-youtube
    icon-social-zerply
    icon-mail

-------------------------------->

<!-- add social info here -->



<nav class="cover-navigation navigation--social">
  <ul class="navigation">

    
      <!-- Github -->
      <li class="navigation__item">
        <a href="https://github.com/ajiu9" title="">
          <i class='icon icon-social-github'></i>
          <span class="label">GitHub</span>
        </a>
      </li>
    

    <!-- China social icon -->
    <!--
    
      <li class="navigation__item">
        <a href="" title="">
          <i class='icon cs-icon-douban'></i>
          <span class="label">Douban</span>
        </a>
      </li>

      <li class="navigation__item">
        <a href="" title="">
          <i class='icon cs-icon-weibo'></i>
          <span class="label">Weibo</span>
        </a>
      </li>

    -->



  </ul>
</nav>



        </div>

      </div>

    </div>

    <div class="panel-cover--overlay"></div>
  </div>
</header>

    <div class="content-wrapper">
        <div class="content-wrapper__inner entry">
            

<article class="post-container post-container--single">

  <header class="post-header">
    
    <h1 class="post-title">DOM事件</h1>

    

    <div class="post-meta">
      <time datetime="2018-09-26" class="post-meta__date date">2018-09-26</time> 

      <span class="post-meta__tags tags">

          
            <font class="categories">
            &#8226; 分类:
            <a class="categories-link" href="/categories/事件/">事件</a>
            </font>
          

          
             &#8226; 标签:
            <font class="tags">
              <a class="tags-link" href="/tags/前端/">前端</a>
            </font>
          

      </span>
    </div>
    
    

  </header>

  <section id="post-content" class="article-content post">
    <h4 id="事件级别"><a href="#事件级别" class="headerlink" title="事件级别"></a>事件级别</h4><ul>
<li>DOM0 <code>element.onclick = function(){}</code></li>
<li>DOM2 <code>element.addEventListener(&#39;click&#39;, function(){}, false)</code></li>
<li>DOM3 <code>element.addEventListener(&#39;keyup&#39;, function(){}, false)</code>在DOM2事件后新增了UI事件、鼠标事件、键盘事件等事件类型</li>
</ul>
<h4 id="事件模型"><a href="#事件模型" class="headerlink" title="事件模型"></a>事件模型</h4><ul>
<li>事件捕获</li>
<li>事件冒泡</li>
</ul>
<h4 id="DOM事件流"><a href="#DOM事件流" class="headerlink" title="DOM事件流"></a>DOM事件流</h4><p>DOM(文档对象模型)结构是一个树型结构，当一个HTML元素产生一个事件时，该事件会在元素节点与根结点之间的路径传播，路径所经过的结点都会收到该事件，这个传播过程可称为DOM事件流。</p>
<h4 id="DOM事件流的具体流程"><a href="#DOM事件流的具体流程" class="headerlink" title="DOM事件流的具体流程"></a>DOM事件流的具体流程</h4><p>在DOM兼容浏览器中，事件流中分三个级段：</p>
<ul>
<li>捕获阶段（Capture Phase）：事件从window自上而下向目标节点传播的阶段；</li>
<li>目标阶段（Target Phase）：正的目标节点正在处理事件的阶段；</li>
<li>冒泡阶段（Bubbling Phase）：事件从目标节点自上而下向window传播的阶段。<br><img src="/images/eventflow.png" alt=""></li>
</ul>
<h4 id="Event对象的常见应用"><a href="#Event对象的常见应用" class="headerlink" title="Event对象的常见应用"></a>Event对象的常见应用</h4><ul>
<li>event.preventDefault() 阻止默认事件</li>
<li>event.stopPropagation() 阻止捕获和冒泡阶段中当前事件的进一步传播</li>
<li>event.stopImmediatePropagation() 阻止事件冒泡并且阻止相同事件的其他侦听器被调用</li>
<li>event.currentTarget 当事件遍历DOM时，标识事件的当前目标。它总是引用事件处理程序附加到的元素(当前绑定的事件元素)</li>
<li>event.target 标识事件发生的元素（当前被点击的元素）</li>
</ul>
<h4 id="自定义事件"><a href="#自定义事件" class="headerlink" title="自定义事件"></a>自定义事件</h4><blockquote>
<p>语法 <code>event = new Event(typeArg, eventInit);</code><br>typeArg 是DOMString 类型，表示所创建事件的名称。<br>eventInit可选,是 EventInit 类型的字典，接受以下字段:</p>
<ul>
<li>“bubbles”，可选，Boolean类型，默认值为 false，表示该事件是否冒泡。</li>
<li>“cancelable”，可选，Boolean类型，默认值为 false， 表示该事件能否被取消。</li>
<li>“composed”，可选，Boolean类型，默认值为 false，指示事件是否会在影子DOM根节点之外触发侦听器。</li>
</ul>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建一个支持冒泡且不能被取消的attach事件</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> ev = <span class="keyword">new</span> Event(<span class="string">"attach"</span>, &#123;<span class="string">"bubbles"</span>:<span class="literal">true</span>, <span class="string">"cancelable"</span>:<span class="literal">false</span>&#125;);</span><br><span class="line"><span class="built_in">document</span>.dispatchEvent(ev);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 事件可以在任何元素触发，不仅仅是document</span></span><br><span class="line">myDiv.dispatchEvent(ev);</span><br></pre></td></tr></table></figure>
<p><code>customEvent</code>事件是由程序创建的，可以有任意自定义功能的事件。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 添加一个适当的事件监听器</span></span><br><span class="line">obj.addEventListener(<span class="string">"cat"</span>, <span class="function"><span class="keyword">function</span>(<span class="params">e</span>) </span>&#123; process(e.detail) &#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建并分发事件</span></span><br><span class="line"><span class="keyword">var</span> event = <span class="keyword">new</span> CustomEvent(<span class="string">"cat"</span>, &#123;<span class="string">"bubbles"</span>: <span class="literal">true</span>, <span class="string">"cancelable"</span>: <span class="literal">false</span>, <span class="string">"detail"</span>:&#123;<span class="string">"hazcheeseburger"</span>:<span class="literal">true</span>&#125;&#125;)</span><br><span class="line">obj.dispatchEvent(event)</span><br></pre></td></tr></table></figure></p>

  </section>

  <section class="post-comments">

   <!--PC和WAP自适应版-->
<div id="SOHUCS" sid="请将此处替换为配置SourceID的语句" ></div> 
<script type="text/javascript"> 
(function(){ 
var appid = 'cytl2q04G'; 
var conf = 'prod_c6e77c75d2d767714e129c11e500044d'; 
var width = window.innerWidth || document.documentElement.clientWidth; 
if (width < 960) { 
window.document.write('<script id="changyan_mobile_js" charset="utf-8" type="text/javascript" src="https://changyan.sohu.com/upload/mobile/wap-js/changyan_mobile.js?client_id=' + appid + '&conf=' + conf + '"><\/script>'); } else { var loadJs=function(d,a){var c=document.getElementsByTagName("head")[0]||document.head||document.documentElement;var b=document.createElement("script");b.setAttribute("type","text/javascript");b.setAttribute("charset","UTF-8");b.setAttribute("src",d);if(typeof a==="function"){if(window.attachEvent){b.onreadystatechange=function(){var e=b.readyState;if(e==="loaded"||e==="complete"){b.onreadystatechange=null;a()}}}else{b.onload=a}}c.appendChild(b)};loadJs("https://changyan.sohu.com/upload/changyan.js",function(){window.changyan.api.config({appid:appid,conf:conf})}); } })(); </script>
    
</section>


</article>


            <footer class="footer">

    <span class="footer__copyright">&copy; 2017 | 由<a href="https://hexo.io/">Hexo</a>| 主题<a href="https://github.com/someus/huno">Huno</a></span>
    
</footer>
        </div>
    </div>

    <!-- js files -->
    <script src="/js/jquery.min.js"></script>
    <script src="/js/main.js"></script>
    <script src="/js/scale.fix.js"></script>
    

    

    <script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <script type="text/javascript"> 
        $(document).ready(function(){
            MathJax.Hub.Config({ 
                tex2jax: {inlineMath: [['[latex]','[/latex]'], ['\\(','\\)']]} 
            });
        });
    </script>


    

    <script src="/js/awesome-toc.min.js"></script>
    <script>
        $(document).ready(function(){
            $.awesome_toc({
                overlay: true,
                contentId: "post-content",
            });
        });
    </script>


    
    
    <!--kill ie6 -->
<!--[if IE 6]>
  <script src="//letskillie6.googlecode.com/svn/trunk/2/zh_CN.js"></script>
<![endif]-->

</body>
</html>
